<template>
  <div class="app-container">
    <doc-alert title="工作流" url="https://doc.iocoder.cn/bpm" />

    <!-- 搜索工作栏 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="请假时间" prop="stime">
        <!-- <el-date-picker
          clearable
          style="width: 340px"
          v-model="stime"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00', '23:59:59']"
          @change="changeTimeData"
        /> -->
        <el-date-picker
          type="date"
          value-format="yyyy-MM-dd"
          v-model="queryParams.leaveStartTime"
          placeholder="开始日期"
        ></el-date-picker
        ><span>-</span
        ><el-date-picker
          type="date"
          v-model="queryParams.leaveEndTime"
          value-format="yyyy-MM-dd"
          placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="审批状态" prop="approvalStatus">
        <el-select
          v-model="queryParams.approvalStatus"
          placeholder="请选择审批状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in this.getDictDatas(
              DICT_TYPE.ATTENDANCE_APPROVAL_STATUS
            )"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!-- v-hasPermi="['bpm:oa-leave:create']" -->
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >请假申请</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="学院" align="center" prop="collegeName" />
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="角色" align="center" prop="userType">
        <template slot-scope="scope">
          <dict-tag
            :type="DICT_TYPE.COLLEGE_USER_ROLE"
            :value="scope.row.userType"
          />
        </template>
      </el-table-column>
      <el-table-column label="联系方式" align="center" prop="userPhone" />
      <el-table-column label="请假天数" align="center" prop="leaveDays" />
      <el-table-column label="请假说明" align="center" prop="leaveExplain" />
      <!-- <el-table-column label="请假开始时间" align="center" prop="leaveExplain" /> -->
      <!-- <el-table-column label="状态" align="center" prop="result">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.BPM_PROCESS_INSTANCE_RESULT" :value="scope.row.result"/>
        </template>
      </el-table-column> -->
      <el-table-column
        label="请假开始时间"
        align="center"
        prop="leaveStartTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaveStartTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="请假结束时间"
        align="center"
        prop="leaveEndTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaveEndTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="approvalStatus">
        <template slot-scope="scope">
          <dict-tag
            :type="DICT_TYPE.ATTENDANCE_APPROVAL_STATUS"
            :value="scope.row.approvalStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      title="查看"
      append-to-body
      :visible.sync="dialogVisible"
      width="30%"
      :close-on-click-modal="false"
    >
      <div>
        <el-row>
          <div class="group-name">基本信息</div>

          <el-col class="item" :span="12"
            >学院:{{ attendanceManagementDetail.collegeName }}</el-col
          >
          <el-col
            class="item"
            :span="12"
            v-if="attendanceManagementDetail.userType == 4"
            >系部:{{ attendanceManagementDetail.departmentName }}</el-col
          >

          <el-col class="item" :span="12"
            >请假人姓名:{{ attendanceManagementDetail.userName }}</el-col
          >
          <el-col class="item" :span="12"
            >联系方式:{{ attendanceManagementDetail.userPhone }}</el-col
          >

          <el-col class="item" :span="12"
            >角色:{{
              attendanceManagementDetail.userType == 4 ? "学生" : "教师"
            }}</el-col
          >
          <el-col class="item" :span="12"
            >请假天数:{{ attendanceManagementDetail.leaveDays }}天</el-col
          >
          <el-col class="item" :span="12"
            >请假开始时间:{{
              attendanceManagementDetail.leaveStartTime
            }}</el-col
          >
          <el-col class="item" :span="12"
            >请假结束时间:{{ attendanceManagementDetail.leaveEndTime }}</el-col
          >
          <el-col class="item" :span="12"
            >请假类型:{{ attendanceManagementDetail.reasonName }}</el-col
          >
          <el-col class="item" :span="12"
            >原因描述:{{ attendanceManagementDetail.leaveExplain }}</el-col
          >
          <el-col class="item" :span="12"
            >审批状态:
            <dict-tag
              :type="DICT_TYPE.ATTENDANCE_APPROVAL_STATUS"
              :value="attendanceManagementDetail.approvalStatus"
            />
          </el-col>
        </el-row>
      </div>
    </el-dialog>
    <create :show.sync="createVisible" @success="getList"></create>
  </div>
</template>

<script>
import { getAttendanceLeaveReasonPage, getAttendanceLeaveData } from "@/api/studentSide/leave"
import { getDictDatas, DICT_TYPE } from '@/utils/dict'
import { cancelProcessInstance } from "@/api/bpm/processInstance";
import create from './create';
export default {
  name: "Leave",
  components: {
    create
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请假申请列表
      list: [],
      stime: '',
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        leaveStartTime: undefined,
        leaveEndTime: undefined,
        approvalStatus: undefined,
      },
      createVisible: false,
      dialogVisible: false,
      attendanceManagementDetail: {},//详情
      leaveTypeDictData: getDictDatas(DICT_TYPE.BPM_OA_LEAVE_TYPE),
      leaveResultData: getDictDatas(DICT_TYPE.BPM_PROCESS_INSTANCE_RESULT),
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      console.log(this.queryParams);
      getAttendanceLeaveReasonPage({ ...this.queryParams, userId: this.$store.getters.userId }).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    //请假申请
    handleAdd() {
      this.createVisible = true;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.leaveStartTime = undefined;
      this.queryParams.leaveEndTime = undefined;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.dialogVisible = true;
      const id = row.id;
      getAttendanceLeaveData(id).then((response) => {
        this.attendanceManagementDetail = response.data;
      });
    },

    changeTimeData(vals) {
      if (vals == null) {
        this.queryParams.leaveStartTime = undefined;
        this.queryParams.leaveEndTime = undefined;
        return;
      }
      this.queryParams.leaveStartTime = vals[0];
      this.queryParams.leaveEndTime = vals[1];
    },
    /** 取消请假 */
    handleCancel(row) {
      const id = row.processInstanceId;
      this.$prompt('请输入取消原因？', "取消流程", {
        type: 'warning',
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /^[\s\S]*.*[^\s][\s\S]*$/, // 判断非空，且非空格
        inputErrorMessage: "取消原因不能为空",
      }).then(({ value }) => {
        return cancelProcessInstance(id, value);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消成功");
      })
    }
  }
};
</script>
<style lang="scss" scoped>
.item {
  padding: 10px 0;
}
.group-name {
  font-size: 18px;
  font-weight: bold;
  border-bottom: 1px solid #ccc;
}
</style>
